package com.elite;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.xml.rpc.holders.StringHolder;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.tempuri.WebServiceTest;
import org.tempuri.WmgwLocator;

import com.dbic.sendMessage.client.MtNewMessage;
import com.dbic.sendMessage.client.SmsNewOperatorService;
import com.dbic.sendMessage.client.SmsNewOperatorServiceLocator;
import com.dbic.sendMessage.client.SmsNewOperator_PortType;
import com.efesco.smssvr.SmsSender;
import com.efesco.smssvr.model.ArrayOfSmsModel;
import com.efesco.smssvr.model.SmsBatchModel;
import com.efesco.smssvr.model.SmsModel;



public class WFSMS implements Runnable{
	private static final Log log=LogFactory.getLog(WFSMS.class);
	public static com.elite.FtpBean ftpbean;
	public static String times;
	private boolean flag = true; 
	private static int Hz =1; 
	private static String sql =" select * from Documentout where DOCTYPE=3 and ((Status=0) or (Status!=1 and TrialCount<TrialCountLimit)) and expectedstartdate<=sysdate "; 
	private static String update_sql="";
	public static String username;
	public static String username1; 
	public static String password;
	public static String password1;
	public static String XHH;
	public static String XHH1;
	private static String LastTryDateTime="";
	private static String FinishDateTime="";
	public static String IP;
	private static int xh_int=0;
	private static SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
	
	public static String getIP() {
		return IP;
	}
	public static void setIP(String ip) {
		IP = ip;
	}
	public static com.elite.FtpBean getFtpbean() {
		return ftpbean;
	}
	public static void setFtpbean(com.elite.FtpBean ftpbean) {
		WFSMS.ftpbean = ftpbean;
	}
	public static String getTimes() {
		return times;
	}
	public static void setTimes(String times) {
		WFSMS.times = times;
	}
	public static String getUsername() {
		return username;
	}
	public static void setUsername(String username) {
		WFSMS.username = username;
	}
	public static String getUsername1() {
		return username1;
	}
	public static void setUsername1(String username1) {
		WFSMS.username1 = username1;
	}
	public static String getPassword() {
		return password;
	}
	public static void setPassword(String password) {
		WFSMS.password = password;
	}
	public static String getPassword1() {
		return password1;
	}
	public static void setPassword1(String password1) {
		WFSMS.password1 = password1;
	}
	public static String getXHH() {
		return XHH;
	}
	public static void setXHH(String xhh) {
		WFSMS.XHH = xhh;
	}
	public static String getXHH1() {
		return XHH1;
	}
	public static void setXHH1(String xhh1) {
		WFSMS.XHH1 = xhh1;
	}


	private String getERRO(Exception e){
		String str="";
		try{
            StringWriter sw = new StringWriter();  
            e.printStackTrace(new PrintWriter(sw, true));  
            str = sw.toString(); 	
		}catch(Exception e1){
			str=e.toString();
			log.error("getERRO:"+e1.toString());
		}
		return str;
	}
	public void run(){
		log.info("---------------------外服短信接口----------------------------");
		while(flag){
			try{
				log.info("sss:"+times);
				Hz=Integer.parseInt(times);
			}catch(Exception e){
				log.info("错误:"+e);
				log.info("因为获取运行频率出错，改为默认频率1分钟运行");
			}
			log.info("扫描documentout表中未处理的记录："+sql);
			List list=null;
			try{
				list=ftpbean.chaxun(sql);
			}catch(Exception e){
				log.error("扫描documentout表_error:"+getERRO(e));
			}
			if(list!=null){
				for(int i=0;i<list.size();i++){
					Map map=(Map)list.get(i); 
					String createdby=Util.filterForObject(map.get("createdby"));
					log.info("createdby:"+createdby);
					String mobile=Util.filterForObject(map.get("DocumentTo"));
					log.info("mobile:"+mobile);
					String DocumentPath=Util.filterForObject(map.get("DocumentPath"));
					log.info("DocumentPath:"+DocumentPath);
					String TrialCountLimit=Util.filterForObject(map.get("TrialCountLimit"));
					log.info("TrialCountLimit:"+TrialCountLimit);
					String DocumentOut_GUID=Util.filterForObject(map.get("DocumentOut_GUID"));
					log.info("DocumentOut_GUID:"+DocumentOut_GUID);
                    try{
    				//创建短信服务对象
    					SmsSender sender = SmsSender.getSingleInstance();	
    				//短信任务对象
    					SmsBatchModel smsbat = new SmsBatchModel();
    		  		//允许重发次数，主信息内容中的allow_retry_cnt
    					smsbat.setAllow_retry_cnt(new Integer(1)); 
    		  		//启动发送时间，主信息内容中的begin_send_date
    					Calendar cal=Calendar.getInstance();
    					cal.setTime(new Date());
    					smsbat.setBegin_send_date(cal); 
    		  		//来源业务类别，主信息内容中的BIZ_TYPE
    					smsbat.setBiz_type("calcnt");
    		  		//是否邮件反馈结果，1是，0否，主信息内容中的is_mail
    					smsbat.setIs_mail("0");
    		  		//发送人邮箱，主信息内容中的send_user_email
    					smsbat.setSend_user_email("aaa@aaa.com");
    		  		//发送人姓名，主信息内容中的send_user_name
    					smsbat.setSend_user_name(createdby);
    		  		//发送人ID，主信息内容中的send_user
    					smsbat.setSend_user("1234");
    		  		//发送部门ID，主信息内容中的send_depts
    					smsbat.setSend_depts("50");
    		  		//发送部门名称，主信息内容中的send_depts_name
    					smsbat.setSend_depts_name("呼叫中心");
    		  		//备注，主信息内容中的fremarks
    					smsbat.setFremarks("备注信息");
    					SmsModel[] smsarr = new SmsModel[1];						
						//短信对象
						SmsModel sms = new SmsModel();
						//手机号，明细信息中的mp				
						sms.setMp(mobile);
						//短信内容，明细信息中的content
						sms.setContent(DocumentPath);
						//接收者ID，明细信息中的rcv_id
						sms.setRcv_id(mobile);
						//接收者类别，明细信息中的rcv_type
						sms.setRcv_type("EMP");					
						//放入短信数组
						smsarr[0] = sms;
						ArrayOfSmsModel smslst = new ArrayOfSmsModel();
						//将短信数组放入包装类对象
						smslst.setSmsModel(smsarr); 
						//将短信数组包装类对象放入短信任务对象
						smsbat.setSmslist(smslst); 						
						//调用接口发送
						long batno = sender.sendSmsBatch(smsbat);
						log.info("batno:"+batno);
						update_sql=" update DocumentOut set Status=1,TrialCount=TrialCount+1,LastTryDateTime=to_date('"+LastTryDateTime+"','yyyy-mm-dd hh24:mi:ss'),FinishDateTime=to_date('"+FinishDateTime+"','yyyy-mm-dd hh24:mi:ss'),ModifiedDate=sysdate where DocumentOut_GUID='"+DocumentOut_GUID+"'";
                    }catch(Exception e){
                    	update_sql=" update DocumentOut set FailCode="+00+",FailDescription='失败',Status=5,TrialCount=TrialCount+1,LastTryDateTime=to_date('"+LastTryDateTime+"','yyyy-mm-dd hh24:mi:ss'),FinishDateTime=to_date('"+FinishDateTime+"','yyyy-mm-dd hh24:mi:ss'),ModifiedDate=sysdate where DocumentOut_GUID='"+DocumentOut_GUID+"'";
                    	log.error(getERRO(e));
                    }
					log.info("update_sql:"+update_sql);
					try{
						int a=ftpbean.update(update_sql);
						log.info("是否更新DocumentOut成功:"+a);
					}catch(Exception e){
						log.info("更新DocumentOut出错:"+e);
					}
					
				}
				
			}else{
				log.info("没有需要发送的短信");
			}
                
			try{
				Thread.sleep(Hz*1000);
			}catch(Exception e){
				log.error("定时错误:"+getERRO(e));
			}
		}
	}

	

}





















